import 'dart:convert';

import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';

import 'JsonBean/shapefile_bean.dart';

class ShapeFileRoute extends StatefulWidget {
  @override
  _ShapeFileRouteState createState() {
    return _ShapeFileRouteState();
  }
}

class _ShapeFileRouteState extends State<ShapeFileRoute> {
  static const platform = const MethodChannel('samples.flutter.io/battery');

  BaseOptions _options = BaseOptions(
      baseUrl: "http://192.168.1.119:8080/pgisserver/shapefile/",
      connectTimeout: 5000,
      receiveTimeout: 5000,
      responseType: ResponseType.plain);
  Dio _dio;

  Future<ShapeFileBean> _getData() async {
    Response response = await _dio.get("getAllShapefile");
    Map<String, dynamic> data = json.decode(response.data.toString());
    ShapeFileBean shapeFileBean = ShapeFileBean.fromJson(data);

    return shapeFileBean;
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _dio = Dio(_options);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("接口测试"),
      ),
      body: FutureBuilder<ShapeFileBean>(
        future: _getData(),
        builder: (context, snapshot) {
          Widget widget;
          if (snapshot.connectionState == ConnectionState.done) {
            if (snapshot.hasError) {
              widget = Text("错误：" + snapshot.error);
            } else {
              widget = ListView.separated(
                  itemBuilder: (context, index) {
                    ShapefileData data = snapshot.data.data[index];
                    return Padding(
                      padding:
                          EdgeInsets.symmetric(vertical: 10, horizontal: 20),
                      child: Row(
                        children: <Widget>[
                          Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: <Widget>[
                              Text(
                                "Id : ${data.id}",
                                style: TextStyle(fontSize: 15),
                              ),
                              Text(
                                "分类: ${data.industryName}",
                                style: TextStyle(fontSize: 12),
                              )
                            ],
                          ),
                          Spacer(),
                          OutlineButton(
                            onPressed: () {
                              platform.invokeMethod("downloading", {
                                "fileName": data.fileName,
                                "shapefile": data.shapeFile
                              }).then((value) {
                                SnackBar s = SnackBar(
                                  content: Text(value),
                                  action: SnackBarAction(
                                      label: "知道了", onPressed: () {}),
                                );
                                Scaffold.of(context).showSnackBar(s);
                              });
                            },
                            child: Text("下载文件"),
                          ),
                        ],
                      ),
                    );
                  },
                  separatorBuilder: (context, index) => Divider(
                        height: 1,
                      ),
                  itemCount: snapshot.data.data.length);
            }
          } else {
            widget = Center(
              child: CircularProgressIndicator(),
            );
          }
          return widget;
        },
      ),
    );
  }
}
